#ifndef SKETCHWIDGET_H
#define SKETCHWIDGET_H

#include <QGLWidget>
#include <QVector>
#include <Point.h>

typedef enum {
    SLOW = 1,
    NORMAL = 3,
    FAST = 6
} Speed;

typedef enum {
    deleteOrAddPoint,
    moveOrChangePoint,
    rotate,
    nothing,
    inRotation
} Mode;

class Window;

class SketchWidget : public QGLWidget
{
    Q_OBJECT

public:
    explicit SketchWidget(QWidget *parent = 0);
    QVector<Point> getPoints();
    QVector<Speed> getSpeeds();
    void deletePointIndice(int i);

    double angle(Point v1, Point v2);
    Point rotatePoint(Point p, double angle);
    Point translatePoint(Point p, double distance);

    void doRotate(double angle);
    void doTranslate(double distance);
    void rotateFov(double angle);
    void updateWindow();
    void setWindow(Window* w);

    Mode getMode();
    void setMode(Mode m);

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w, int h);
    void mousePressEvent(QMouseEvent* evt);
    void mouseMoveEvent(QMouseEvent* evt);
    void mouseReleaseEvent(QMouseEvent* evt);

private:
    void drawGrid();
    void drawPath();
    void drawTempPath();
    void drawOldPath();
    void drawRobot();
    void drawFov();
    void drawCircleRotation();
    void drawEraserLine();
    Point mouseTo2D(int x, int y);
    void addPoint(Point p);
    void deletePoint(Point p1, Point p2);
    void changePointSpeed(int i);
    int isNearPoint(Point p);
    bool isOnRobotBody(Point p);
    bool ccw(Point A, Point B, Point C);
    bool intersect(Point A, Point B, Point C, Point D);
    Point pointOnCircleRotation(Point A, float r);

signals:
    
public slots:
    
private:
    QVector<Point> m_points;
    QVector<Point> m_oldPoints;
    QVector<Speed> m_speeds;

    Point viewDirectionLeft;
    Point viewDirectionRight;

    Mode mode;

    int selectedPoint;
    bool b_move;

    Point p1EraserLine;
    Point p2EraserLine;

    Point mouseRotate;
    double angleRotate;
    double oldAngleRotate;

    Window* mainWindow;
};

#endif // SKETCHWIDGET_H
